package com.zm.zmgz.controller.systemSettings;

import com.zm.zmgz.pojo.MetadataBean;
import com.zm.zmgz.pojo.Response;
import com.zm.zmgz.service.MetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/05/11/12:22
 * @Description:元数据
 */
@RestController
@RequestMapping("/metadata")
public class MetadataCotorller {

    @Autowired
    MetadataService metadataService;


    /***
     * @Description: 通过name获取元数据数组
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-11 12:25:35
     */
    @GetMapping("/getMetaDateByName")
    public Response getMetaDateByName(String name){
        List<MetadataBean> list = metadataService.getMetaDateByName(name);
        return Objects.isNull(list) ? new Response(20000, "元数据为空!", null) : new Response(20000, "获取所有元数据成功!", list);
    }

    /***
     * @Description: 获取所有节点
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-11 12:25:35
     */
    @GetMapping("/getAllNode")
    public Response getAllNode(){
        MetadataBean metadataBean = metadataService.getAllNode();
        return Objects.isNull(metadataBean) ? new Response(20000, "元数据为空!", null) : new Response(20000, "获取所有元数据成功!", metadataBean.getChildrenNodes());
    }

    /***
    * @Description: 添加节点
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-05-11 12:25:35
    */
    @PostMapping("/addNode")
    public Response addNode(@RequestBody MetadataBean metadataBean){
        if(Objects.isNull(metadataBean.getPath()) || Objects.isNull(metadataBean.getName()) || Objects.isNull(metadataBean.getChinesename()))
            return new Response(20000, "节点添加失败!", false);
        int i  = metadataService.addNode(metadataBean);
        return i > 0 ? new Response(20000, "节点添加成功!", true) : new Response(20000, "节点添加失败!", false);
    }

    /***
     * @Description: 删除节点及子节点
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-11 12:25:35
     */
    @DeleteMapping("/deleteNode")
    public Response deleteNode(@RequestBody MetadataBean metadataBean){
        int i  = metadataService.deleteNode(metadataBean);
        return i > 0 ? new Response(20000, "节点删除成功!", true) : new Response(20000, "节点删除失败!", false);
    }




    /***
     * @Description: 修改节点的中文名,英文名
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-11 12:25:35
     */
    @PutMapping("/editNode")
    public Response editNode(@RequestBody MetadataBean metadataBean){
        int i  = metadataService.editNode(metadataBean);
        return i > 0 ? new Response(20000, "节点修改成功!", true) : new Response(20000, "节点修改失败!", false);
    }













}
